package com.wit.dao.impl;

import java.util.List;

import com.wit.dao.NoticeDao;
import com.wit.po.Client;
import com.wit.po.Notice;

import hibernate.HibernateSessionFactory;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

public class NoticeDaoImpl implements NoticeDao {

	
	public boolean addNotice(Notice notice) {
		
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.save(notice);
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public Notice getNoticeById(Integer noticeId) {
		
		if(noticeId == null)
			return null;
		Session session = null;
		Notice notice = null;
		try {
			session = HibernateSessionFactory.getSession();
			
			String hql = "from Notice notice where notice.noticeid=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, noticeId);
			
			notice = (Notice)query.uniqueResult();
			
			if(notice.getEmployee().getDepartment() != null)
			{
				System.out.println(notice.getEmployee().getDepartment().getDepartNameC());
			}
			else
				System.out.println("nulll");
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session != null && session.isOpen())
				session.close();
		}
		return notice;
	}

	
	public int getTotalAmount()
	{
		Session session = null;
		Integer totalAmount = 0;
		try {
			session = HibernateSessionFactory.getSession();
			Criteria criteria = session.createCriteria(Notice.class);
	
			totalAmount = (Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return totalAmount;
	}
	
	public static List<Notice> s_GetNoticeList(Integer pageNumber, Integer pageSize) {

		if(pageSize == null || pageNumber == null)
			return null;
		Session session = null;
		List<Notice> noticeList = null;
		try {
			String hql = "from Notice notice order by notice.noticeDate desc";
			session = HibernateSessionFactory.getSession();
			
			Query query = session.createQuery(hql);
			
			query.setFirstResult((pageNumber-1)*pageSize);
			query.setMaxResults(pageSize);
			
			noticeList = query.list();
		
		} catch (Exception e) {

			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return noticeList;
	}
	
	
	public List<Notice> getNoticeList(Integer pageNumber, Integer pageSize) {
		
		return s_GetNoticeList(pageNumber, pageSize);
	}

	
	public boolean removeNotice(Notice notice) {
		
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.delete(notice);
			
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

	
	public boolean updateNotice(Notice notice) {

		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			
			session.update(notice);
			
			tx.commit();
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}

}
